/*
3. 编程题
        使用线程池将一个目录中的所有内容拷贝到另外一个目录中，包含子目录中的内容。
*/


import java.io.*;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Q3 implements Runnable{

    private File subPath;
    private File subDst;

    public Q3(File subPath, File subDst) {
        this.subPath = subPath;
        this.subDst = subDst;
    }

    @Override
    public void run() {
        try {
            fileCopy(subPath.getAbsolutePath(), subDst.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //使用缓冲字节流赋值src文件到dst位置
    public static void fileCopy(String src, String dst) throws IOException {
        System.out.println("开始拷贝" + src + '到' + dst);
        BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(src));
        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(dst));
        byte[] bArr = new byte[1024];
        int res = 0;
        while((res = inputStream.read(bArr)) != -1) {
            outputStream.write(bArr,0,res);
        }
        outputStream.close();
        inputStream.close();
        System.out.println("拷贝完成！！！");
    }
    public static void recursiveCopy(File src, File dst) {
        if (!dst.isDirectory()) {dst.mkdir();}  //判断目录是否存在
        for(File subPath: Objects.requireNonNull(src.listFiles())) {
            File subDst = new File(dst, subPath.getName());
            if (subPath.isFile()) {
                try {
                    fileCopy(subPath.getAbsolutePath(), subDst.getAbsolutePath());
                } catch (IOException e) {
                    e.printStackTrace();
                    return;
                }
            }
            if (subPath.isDirectory()) {
                recursiveCopy(subPath,subDst);
            }
        }
    }


    public static void recursiveCopyMultiThread(File src, File dst, ExecutorService executorService) {
        if (!dst.isDirectory()) {dst.mkdir();}  //判断目录是否存在
        for(File subPath: Objects.requireNonNull(src.listFiles())) {
            File subDst = new File(dst, subPath.getName());
            if (subPath.isFile()) {
                executorService.execute(new Q3(subPath, subDst));
            }
            if (subPath.isDirectory()) {
                recursiveCopyMultiThread(subPath,subDst, executorService);
            }
        }
    }
    public static void main(String[] args) {
        System.out.println("拷贝tst目录到tst3");
        String path_src = "./code/tst";
        String path_dst = "./code/tst3";
        File src = new File(path_src);
        File dst = new File(path_dst);
        if (!src.isDirectory()) {
            System.out.println(path_src + "不是目录");
        }
        //
        //recursiveCopy(src, dst);
        // 创建一个线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        recursiveCopyMultiThread(src, dst, executorService);
        // 关闭线程池
        executorService.shutdown();
    }
}
